﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PerformanceAgreement : System.Web.UI.Page
    {
        private Main main = new Main();
        private SsMng creatby = new SsMng();
        private IPerformanceAgreementBLL CreatPerformanceAgreement = new PerformanceAgreementBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            ChangeMode(Main.EditMode.View);


            // debug 
            lblEmpSeq.Text = creatby.EmpSeq;
        }

        protected void dgtPerAgreement_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            lblEmpSeq.Text = creatby.EmpSeq;
            string strQueryString = lblEmpSeq.Text;

            dgtPerAgreement.DataSource = CreatPerformanceAgreement.Search(strQueryString, null, null, null, null, null);
        }

        private void LoadPerformanceAgreement()
        {
            lblEmpSeq.Text = creatby.EmpSeq;
            string strQueryString = lblEmpSeq.Text;

            dgtPerAgreement.DataSource = CreatPerformanceAgreement.Search(strQueryString, null, null, null, null, null);
            dgtPerAgreement.DataBind();
        }

        protected void dgtPerAgreement_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "GridDelete")
            {
                GridDataItem item = (GridDataItem) e.Item;

                lblAgreementSeq.Text = item["perf_agreement_seq"].Text;
            }
            else if (e.CommandName == "GridEdit")
            {
                ChangeMode(Main.EditMode.Edit);

                GridDataItem item = (GridDataItem) e.Item;

                txtGoal.Text = item["goal_objective"].Text;
                txtAction.Text = item["action_plan"].Text;
                txtRemark.Text = item["remark"].Text;
                lblAgreementSeq.Text = item["perf_agreement_seq"].Text;

            }
        }

        private void ChangeMode(Main.EditMode mode)
        {
            switch (mode)
            {
                case Main.EditMode.AddNew:
                    pnlAgreeShow.Visible = false;
                    pnlAgreeAdd.Visible = true;
                    Save_Edit.Visible = false;
                    Save_AddNew.Visible = true;
                    ShowGrid.Visible = true;
                    AddNew.Visible = false;
                    Delete.Visible = false;

                    break;

                case Main.EditMode.View:
                    pnlAgreeShow.Visible = true;
                    pnlAgreeAdd.Visible = false;
                    Save_AddNew.Visible = false;
                    Save_Edit.Visible = false;
                    ShowGrid.Visible = true;
                    AddNew.Visible = true;
                    Delete.Visible = true;

                    break;

                case Main.EditMode.Edit:
                    pnlAgreeShow.Visible = false;
                    pnlAgreeAdd.Visible = true;
                    Save_AddNew.Visible = false;
                    ShowGrid.Visible = true;
                    AddNew.Visible = false;
                    Save_Edit.Visible = true;
                    Delete.Visible = false;

                    break;
            }
        }

        private void ClearControlData()
        {
            txtGoal.Text = "";
            txtAction.Text = "";
            txtRemark.Text = "";
        }

        private bool CheckNullValue()
        {
            bool bOk = true;

            if (txtGoal.Text == "")
            {
                main.MessageAlert(Page, "กรุณากรอกเป้าหมาย");
                bOk = false;
            }
            else if (txtAction.Text == "")
            {
                main.MessageAlert(Page, "กรุณากรอกข้อมูล");
                bOk = false;
            }

            return bOk;
        }


        protected void ShowGrid_Click(object sender, EventArgs e)
        {
            ChangeMode(Main.EditMode.View);
            LoadPerformanceAgreement();
        }

        protected void AddNew_Click(object sender, EventArgs e)
        {
            ClearControlData();
            ChangeMode(Main.EditMode.AddNew);
        }

        protected void Save_AddNew_Click(object sender, EventArgs e)
        {
            lblEmpSeq.Text = creatby.EmpSeq;
            string strQueryString = lblEmpSeq.Text;
            string strGoal = txtGoal.Text;
            string strAction = txtAction.Text;
            string strRemark = txtRemark.Text;

            if (!CheckNullValue())
            {

            }
            CreatPerformanceAgreement.Insert(strQueryString, strGoal, strAction, strRemark, creatby.EmpSeqI);

            LoadPerformanceAgreement();
            ChangeMode(Main.EditMode.View);
        }

        protected void Save_Edit_Click(object sender, EventArgs e)
        {
            int? intAgreementSeq = main.ConvertNullInt(lblAgreementSeq.Text);
            lblEmpSeq.Text = creatby.EmpSeq;
            string strQueryString = lblEmpSeq.Text;
            string strGoal = txtGoal.Text;
            string strAction = txtAction.Text;
            string strRemark = txtRemark.Text;
            if (!CheckNullValue())
            {

            }
            CreatPerformanceAgreement.Update(strQueryString, strGoal, strAction, strRemark, creatby.EmpSeqI,
                                             intAgreementSeq);

            LoadPerformanceAgreement();
            ChangeMode(Main.EditMode.View);
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            if (pnlAgreeShow.Visible == true)
            {
                foreach (GridDataItem item in dgtPerAgreement.SelectedItems)
                {
                    int? intAgreementSeq = main.ConvertNullInt(item["perf_agreement_seq"].Text);
                    string strQueryString = main.WebString(item["emp_seq"].Text);

                    CreatPerformanceAgreement.Delete(creatby.EmpSeqI, intAgreementSeq, strQueryString);
                }
            }

            LoadPerformanceAgreement();
        }

    }

}
